package net.wanho.controller;

import lombok.RequiredArgsConstructor;
import net.wanho.dto.ProductDTO;
import net.wanho.po.Product;
import net.wanho.po.Store;
import net.wanho.service.*;
import net.wanho.vo.PageInfo;
import net.wanho.vo.ProductVO;
import net.wanho.vo.R;
import org.apache.ibatis.annotations.Param;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequiredArgsConstructor
@RequestMapping("/product")
public class ProductController {
    private final ProductService productService;
    private final StoreService storeService;
    private final BrandService brandService;
    private final ProductTypeService productTypeService;
    private final UnitService unitService;
    private final SupplyService supplyService;
    private final PlaceService placeService;
    @GetMapping("/product-page-list")
    public R productPageList(@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "5") Integer pageSize, ProductDTO productDTO){
        PageInfo<ProductVO> productList = productService.productPageList(pageNum,pageSize,productDTO);
        return R.success(productList);
    }
    /**
     * 商品类型列表
     */
    @GetMapping("/category-tree")
    public R productTypeList(){
        return R.success(productTypeService.typeList());
    }

    @PostMapping("/product-add")
    public R productAdd(@RequestBody Product product){
        product.setUpDownState("0");
        productService.save(product);
        return R.success("添加成功");
    }

    @GetMapping("/{id}")
    public R productDetail(@PathVariable Integer id){
        Product product = productService.getById(id);
        return R.success(product);
    }

    @PutMapping("/product-update")
    public R productUpdate(@RequestBody Product product){
        productService.updateById(product);
        return R.success("修改成功");
    }

    @DeleteMapping("/product-delete/{id}")
    public R productDelete(@PathVariable Integer id){
        productService.removeById(id);
        return R.success("删除成功");
    }

    @DeleteMapping("/product-list-delete")
    public R productDeleteBatch(@RequestBody List<Integer> ids){
        productService.removeByIds(ids);
        return R.success("批量删除成功");
    }

    @GetMapping("/exportTable")
    public R exportTable(@RequestParam(defaultValue = "1") Integer pageNum,@RequestParam(defaultValue = "5") Integer pageSize, ProductDTO productDTO){
        PageInfo<ProductVO> productList = productService.productPageList(pageNum,pageSize,productDTO);
        return R.success(productList.getResultList());
    }

    @PutMapping("/state-change")
    public R stateChange(@RequestBody Product product){
        productService.updateById(product);
        return R.success("修改成功");
    }


}
